package cn.tedu.xuecha.service.impl;

import cn.tedu.xuecha.entity.dto.CommentCategoryDTO;
import cn.tedu.xuecha.entity.pojo.CommentCategory;
import cn.tedu.xuecha.ex.ServiceException;
import cn.tedu.xuecha.mapper.CommentCategoryMapper;
import cn.tedu.xuecha.respone.AppCode;
import cn.tedu.xuecha.service.CommentCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author coinyang
 * @date 2023/4/23 11:48
 */
@Service
public class CommentCategoryImpl implements CommentCategoryService {

    @Autowired
    CommentCategoryMapper commentCategoryMapper;

    @Override
    public void addNewCategory(CommentCategoryDTO commentCategoryDTO) {
        if(commentCategoryDTO ==null){
            String message = "新建类目不能为空";
            throw new ServiceException( AppCode.ERR_CONFLICT,message);
        }
        CommentCategory commentCategory = new CommentCategory();
        BeanUtils.copyProperties(commentCategoryDTO,commentCategory);
        int rows = commentCategoryMapper.insertSelective(commentCategory);
        if(rows==0){
            String message="新增类目失败";
            throw  new ServiceException(AppCode.ERR_INSERT,message);
        }
    }

    @Override
    public List<CommentCategory> selectList() {
        return commentCategoryMapper.list();
    }
}
